// xc_lshop/index/com/default.js
var actbill = require('../../../xc/model/pro_contents')
var actbillclass = require('../../../xc/model/pro_class')
const app = getApp()

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    config: {
      type: Object,
      value: {},
      observer: function (newVal, oldVal) {
        if (newVal) {
          this.init()
        }

        // 属性值变化时执行
      }
    },
  },


  /**
   * 组件的初始数据
   */
  data: {
    multipleitems:3,
    imgslist: ["https://img.yzcdn.cn/vant/cat.jpeg"],
    prolist: [],
    actpidx: -1,
    actspid: 0,
    actpid: 0,
    seachval: "",
    CustomBar: app.globalData.CustomBar,
    selected: {}
  },

  ready() {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    getindex(){
      
      var that = this
      actbill.getindexactivity().then(res => {

        if (res['status'] == 1) {
          that.setData({
            activityids: res['obj']
          })
        }
      })
    },
    buyactvitypro(e) {
      console.log("e")
      console.log(e)
      var activity = e.detail.activity;
      //活动未开始/结束/下架不能购买 达到上限
      if ((!activity) || activity.actstatus != 1) {
        return;
      }
      //  if (!this.data.memberinfo) {
      //    this.triggerEvent("login","");
      //  }



      // var dataset = ev.currentTarget.dataset


      var that = this
      var option = {
        aid: activity.id,
        atype: activity.itype
      }
      var proid = e.detail.proid;
      // console.log("dataset.id")
      // console.log(dataset.id)
      // console.log(that.data.selected[dataset.id])
      // console.log(option)
      actbill.showattrchose(that, proid, that.data.selected[proid], activity.itype == 3 ? 2 : 3, option)
    },
    buy: function (ev) {

      var that = this
      var dataset = ev.detail.dataset
      var item = dataset.item


      // 1加购 2立即购买 3两种
      if (item.contrebate && item.tagsids.indexOf("-1") != -1 && this.data.configrebate['status'] == 1) {
        console.log("item.contrebate")
        var option = {
          aid: -1001,
          atype: -1001
        }
        actbill.showattrchose(that, item.id, null, 2, option)

      } else {
        if (this.data.cartstatus == '1') {
          actbill.showattrchose(that, item.id, null, 3)
        } else {
          actbill.showattrchose(that, item.id, null, 2)
        }
      }
      // var dataset = ev.detail.dataset;

    },
    //滚动到底部
    upper(e) {

      var that = this
      this.getpagelist(that, -1)
    },
    tabschangclass: function (ev) {

      this.changclass({
        detail: ev.detail.name
      })

    },
    changclass: function (ev) {

      var treeclass = this.data.treeclass;
      var pidlist = null
      if (treeclass.level == "1") {
        pidlist = treeclass.modellist;
      } else {
        pidlist = treeclass.tree;
      }
      this.data.actpidx = ev.detail
      if (this.data.actpidx == 0) {
        this.data.actpid = 0
      } else {
        this.data.actpid = pidlist[(this.data.actpidx - 1)].id
      }
      this.setData({
        actpidx: this.data.actpidx,
        actspid: 0
      })
      var that = this;
      this.getpagelist(that, 1)
    },
    init() {
      this.getindex()
      if (this.data.config) {
        var config = this.data.config
        if (config.rebate) {
          // configupdate['configrebate']=config.rebate;
          this.setData({
            configrebate: config.rebate
          })
        } else {
          this.setData({
            configrebate: {
              status: "-1"
            }
          })
        }

        config.prothem.prolist.customStyle = {
          customActiveStyle: "background:#ffffff repeat;color:#FE5909;",
          customBuyStyle: "color:#FE5909;",
          customHeaderStyle: "background:#ffffff repeat",
          customImgStyle: "border-radius:5px 5px 5px 5px;width:125px;margin-left:10px;height:125px",
          customInputStyle: "color:#303133",
          customItemStyle: "background:#ffffff repeat;;border-radius:5px 5px 5px 5px;bottom:0px;margin-top:10px;margin-bottom:0px;",
          customNavStyle: "background:#ffffff repeat",
          customNavitemStyle: "background:repeat;color:#303133;",
          customPageStyle: "background:#F5F5F5 repeat",
          customPriceStyle: "color:#909399;",
          customRealpriceStyle: "color:#FE5909;",
          customSearchStyle: "background:#f7f8fa repeat;color:#303133;border-radius:12px 12px 12px 12px",
          customSecCheckedStyle: "background:#ffffff repeat;color:#FE5909;box-shadow:0px 1px 0px 0px #FE5909",
          customSecNavStyle: "border-radius:5px 5px 5px 5px;margin:8px 8px 8px 8px;background:#ffffff repeat",
          customSecShowStyle: "background:#ffffff repeat;box-shadow:0px 2px 10px 0px rgba(0, 0, 0, 0.1)",
          customSecUncheckedStyle: "background:#ffffff repeat;color:#303133;",
          customTitleStyle: "color:#303133;"
        }

        config.prothem.prolist.proconfig = {
          left: {
            navmode: "top"
          },
          pro: {
            aboutcolor: "#909399",
            aboutrows: "1",
            button: {
              color: "#FE5909",
              height: "18",
              icon: "add",
              show: "1",
              type: "default",
              width: "18",
              background: {
                changebg: {
                  fcolor: "",
                  scolor: "",
                },
                color: "#ffffff",
                h: "25",
                image: "",
                repeat: "repeat",
                size: "cover",
                type: "monochrome",
                w: "25",
                x: "25",
                y: "25",
              }
            },
            colorblock: {
              show: "1",
              num: "3",
              scaleh: "1"
            },
            columns: "2",
            gutter: "16",
            isshowabout: "1",
            mainpic: {
              scaleh: "1",
              "border-radius": {
                "bottom-left": {
                  val: "5",
                  unit: "px"
                },
                "bottom-right": {
                  val: "5",
                  unit: "px"
                },
                "top-left": {
                  val: "5",
                  unit: "px"
                },
                "top-right": {
                  val: "5",
                  unit: "px"
                },
              },
              width: "125",
              mariginleft: "6",
              marginleft: "10"
            },
            mode: "row",
            titlerows: "2",
          },
          secnav: {
            row: "2",
            outbgcolor: "#FE5909"
          }
        }
      } else {
        return;
      }
      if (config.prothem && config.prothem.themcolor) {
        this.setData({
          themcolor: config.prothem.themcolor.config
        })
      }
      if (config.prothem && config.prothem.prolist) {
        var proconfig = config.prothem.prolist.proconfig;
        var customStyle = config.prothem.prolist.customStyle;
        var popconfig = config.prothem.popup.proconfig;
        var popCustomStyle = config.prothem.popup.customStyle;

        this.setData({
          proconfig: proconfig,
          navtype: proconfig.left.navmode,
          customStyle: customStyle,
          popconfig: popconfig,
          popCustomStyle: popCustomStyle
        })
      }

      this.gethotsale()
      this.getallclass()
      var that = this
      var strwhere = {}
      strwhere["status"] = 1
      that.getpagelist(that, 1, strwhere)


    },
    gethotsale() {
      var that = this
      actbill.gethotsale().then(res => {

        if (res['status'] == 1) {
          that.setData({
            hotlist: res['obj']['modellist']
          })
        }
      })
    },
    getallclass() {
      var that = this
      //加载全部分类
      actbillclass.apiall().then(item => {
        that.setData({
          treeclass: item.obj,
          actpidx: 0
        })
        if (that.data.actpid) {
          that.getactpidx(that.data.actpid)
        }
        if (that.data.actspid) {
          // if(that.data.navtype=='left'){
          that.selectComponent("#protopnav").setactcid(that.data.actspid)
          // }else{
          //   that.selectComponent("#imagenav").setactcid(that.data.actspid)
          // }
        }
      })
    },
    getactpidx(actpid) {
      var treeclass = this.data.treeclass;
      var pidlist = null
      if (treeclass.level == "1") {
        pidlist = treeclass.modellist;
      } else {
        pidlist = treeclass.tree;
      }
      for (var i in pidlist) {
        if (pidlist[i].id == actpid) {
          var index = i * 1 + 1
          this.setData({
            activeKey: index,
            actpidx: index
          })
        }
      }
    },
    getpagelist(that, rest, strwhere) {
      if (rest == 1) {
        if (!strwhere) {
          strwhere = {};
        }

        strwhere["status"] = 1;
        strwhere["bpid"] = this.data.actpid;
        strwhere["spid"] = this.data.actspid;
        strwhere["seachval"] = this.data.seachval;
        that.setData({
          modellist: []
        });
      }

      actbill.offset(that, rest, strwhere).then(function (result) {
        var updatemdoel = {};

        if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

          for (var a in result["obj"]["modellist"]) {
            if (result["obj"]["modellist"][a]) {
              result["obj"]["modellist"][a]["snapshot"] = JSON.parse(result["obj"]["modellist"][a]["snapshot"])
            }
          }
          if (rest == 1) {
            updatemdoel["modellist"] = result["obj"]["modellist"];
          } else {
            if (result["obj"]["modellist"].length > 0) {
              updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
            }
          }
        }
        wx.stopPullDownRefresh();
        that.setData(updatemdoel);
      });
    },
  }
})